<?php
namespace Admin\Controller;
use Think\Controller;
class PriceController extends Controller {

	//新增公众号报价
	public function PriceAdd(){					
		$data['single_price'] = I('post.single');					
		$data['one_price'] = I('post.first');					
		$data['two_price'] = I('post.second');					
		$data['three_price'] = I('post.third');				
		$public_id         = I('post.selOpt');


		if(!empty($public_id)){
			$ch_id     = M('wechart_public')->where("public_id=$public_id")->field('id')->find();
			$ch_id 	   = $ch_id['id'];
			$corporate = M('channel')->where("id=$ch_id")->field("corporate_name")->find();
			$data['corporate_name']  = $corporate['corporate_name'];
			$data['public_id']		 = $public_id;

			$pub_price = M('public_price')->add($data);
			if($pub_price){
				$ajaxReturn['code'] = '1';
    			$ajaxReturn['msg']  = 'SUCCESS';
    			$ajaxReturn['data'] = '新增公众号报价成功';
    			$this->ajaxReturn($ajaxReturn);
			}else{
				$ajaxReturn['code'] = '2';
    			$ajaxReturn['msg']  = 'FAIL';
    			$ajaxReturn['data'] = '新增公众号报价失败';
    			$this->ajaxReturn($ajaxReturn);
			}
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '没有选择公众号';
			$this->ajaxReturn($ajaxReturn);
		}
	
	}



	//微信公众号报价列表展示
	public function pub_rice(){
		if($_SERVER['REQUEST_METHOD'] == "GET"){

			$public_price = M('public_price');
	        //查询数据总条数
	        $number      = $public_price->alias('a')->join('table_wechart_public c ON c.public_id= a.public_id')->field('corporate_name,c.name,price_id,one_price,two_price,three_price,single_price')->count();

	        //设置每页条数
	        $page_size = I('get.page_size');
	        //计算可以分为多少页
	        $page_count = ceil($number/$page_size);
	        //接收当前页
	        $page     =  I('get.page');
	        
	        $start    = ($page-1)*$page_size;

			$price = $public_price->alias('a')->join('table_wechart_public c ON c.public_id= a.public_id')->field('corporate_name,c.name,price_id,one_price,two_price,three_price,single_price')->limit($start,$page_size)->select();

			if(!empty($price)){
				$data['page']['total_page'] = $page_count;
            	$data['list'] = $price;

	            $ajaxReturn['code'] = 1;
	            $ajaxReturn['msg'] = "SUCCESS";
	            $ajaxReturn['data'] = $data;
				$this->ajaxReturn($ajaxReturn);
			}else{
				$ajaxReturn['code'] = 2;
	            $ajaxReturn['msg'] = '数据为空';
				$this->ajaxReturn($ajaxReturn);
			}
		}
	}



	//展示修改公众号报价页面
    public function update_price(){
    	$price = I('get.id');						//公众号名称
    	if(!empty($price)){
    			$pub_price   = M('public_price')->where("price_id=$price")->field('price_id,one_price,two_price,three_price,single_price')->find();
	    		$ajaxReturn['code'] = '1';
				$ajaxReturn['msg']  = 'SUCCESS';
				$ajaxReturn['data'] = $pub_price;
				$this->ajaxReturn($ajaxReturn);
	    }else{
	    		$ajaxReturn['code'] = '2';
				$ajaxReturn['msg']  = 'FAIL';
				$ajaxReturn['data'] = '没有选择公众号';
				$this->ajaxReturn($ajaxReturn);
	    }
    }



    //修改公众号报价
    public function update_pro(){
    	$data['single_price'] = I('post.single');			
    	$data['one_price']    = I('post.first');			
		$data['two_price']    = I('post.second');			
		$data['three_price']  = I('post.third');		    
		$price    			  = I('post.id');	

   		$update_pub = M('public_price')->where("price_id=$price")->save($data);
		if($update_pub){
			$ajaxReturn['code'] = '1';
			$ajaxReturn['msg']  = 'SUCCESS';
			$this->ajaxReturn($ajaxReturn);
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '修改失败';
			$this->ajaxReturn($ajaxReturn);
		}
    }




    //删除公众号操作
    public function delete(){
    	$public 		 = I('get.id');       //公众号名称
    	$delete_pub = M('public_price')->where("price_id=$public")->delete();
		if($delete_pub){       //如果成功返回200，如果是失败返回10007
			$ajaxReturn['code'] = '1';
			$ajaxReturn['msg']  = 'SUCCESS';
			$this->ajaxReturn($ajaxReturn);
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '删除失败';
			$this->ajaxReturn($ajaxReturn);
		}
    }

    //查询已有的公众号
	public function wechart_public(){
		if($_SERVER['REQUEST_METHOD'] == "GET"){
			$public = M('wechart_public')->field('name,public_id')->select();
			if($public){
				$ajaxReturn['code'] = '1';
				$ajaxReturn['msg']  = 'SUCCESS';
				$ajaxReturn['data'] = $public;	
			}else{
				$ajaxReturn['code'] = '2';
				$ajaxReturn['msg']  = 'FAIL';
				$ajaxReturn['data'] = '公众号失败';
			}
		}else{
			$ajaxReturn['code'] = '2';
			$ajaxReturn['msg']  = 'FAIL';
			$ajaxReturn['data'] = '请求方式错误';
		}
		$this->ajaxReturn($ajaxReturn);
	}


}